<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item> -->

      <!-- <el-form-item label="商户" prop="shopId">
        <el-select v-model="dataForm.shopId" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.nikeName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <!-- <el-form-item label="摘要" prop="abstractInfo">
      <el-input v-model="dataForm.abstractInfo" placeholder="摘要"></el-input>
    </el-form-item> -->
      <el-form-item label="图片" prop="images">
        <el-upload
          ref="upload"
          :action="uploadUrl"
          :limit="1"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="handleSuccess"
          :file-list="fileList"
          :on-exceed="handlexcedd"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog append-to-body :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <!-- <el-form-item label="时间" prop="currTime">
      <el-input v-model="dataForm.currTime" placeholder="时间"></el-input>
    </el-form-item>
    <el-form-item label="详细介绍" prop="desc">
      <el-input v-model="dataForm.desc" placeholder="详细介绍"></el-input>
    </el-form-item>
    <el-form-item label="点击量" prop="hits">
      <el-input v-model="dataForm.hits" placeholder="点击量"></el-input>
    </el-form-item>
    <el-form-item label="商户名称" prop="shopName">
      <el-input v-model="dataForm.shopName" placeholder="商户名称"></el-input>
    </el-form-item>
     -->
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in statusOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import OssUpload from "../oss/oss-upload.vue";

export default {
  data() {
    return {
      fileList: [],
      uploadFile: {
        //上传的文件对象
        id: 0,
        url: "",
      },
      uploadUrl: this.$http.adornUrl(
        `/testing/file/uploadFile?token=${this.$cookie.get("token")}`
      ),
      uploadUrledit: this.$http.adornUrl(
        `/testing/file/uploadedit?token=${this.$cookie.get("token")}`
      ),
      dialogImageUrl: "",
      dialogVisible: false,
      statusOption: [
        {
          value: 1,
          label: "正常",
        },
        {
          value: 0,
          label: "下架",
        },
      ],
      option: [],
      visible: false,
      dialogVisible: false,
      dataForm: {
        id: 0,
        createTime: "",
        updateTime: "",
        status: "",
        shopId: "",
        title: "",
        abstractInfo: "",
        images: "",
        currTime: "",
        desc: "",
        hits: "",
        shopName: "",
      },

      dataRule: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "状态 1正常 0冻结不能为空",
            trigger: "blur",
          },
        ],
        shopId: [
          { required: true, message: "商户id不能为空", trigger: "blur" },
        ],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        abstractInfo: [
          { required: true, message: "摘要不能为空", trigger: "blur" },
        ],
        images: [{ required: true, message: "图片不能为空", trigger: "blur" }],
        currTime: [
          { required: true, message: "时间不能为空", trigger: "blur" },
        ],
        desc: [
          { required: true, message: "详细介绍不能为空", trigger: "blur" },
        ],
        hits: [{ required: true, message: "点击量不能为空", trigger: "blur" }],
        shopName: [
          { required: true, message: "商户名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: {
    OssUpload,
  },
  methods: {
    handlexcedd() {
      this.$message.error("一次只能上传一张图片! ");
    },
    //图片上传成功的狗子
    handleSuccess(response, file, fileList) {
      if (response.code != 0) {
        //失败
        this.$message.error(response.msg);
        return;
      }
      console.log(this.uploadFile);
      this.uploadFile.id = file.uid;
      this.fileList = fileList;
      this.uploadFile.url = response.data.fileUrl;
      this.$message.success("上传成功！");
    },
    //文件删除
    handleRemove(file, fileList) {
      this.uploadFile = {};
      this.dataForm.images = null;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/testing/shop/list"),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.options = data.page.list;
        }
      });
    },
    init(id) {
      this.getDataList();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.fileList = [];
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/testing/banner/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.createTime = data.banner.createTime;
              this.dataForm.updateTime = data.banner.updateTime;
              this.dataForm.status = data.banner.status;
              this.dataForm.shopId = data.banner.shopId;
              this.dataForm.title = data.banner.title;
              this.dataForm.abstractInfo = data.banner.abstractInfo;
              this.dataForm.images = data.banner.images;
              this.dataForm.currTime = data.banner.currTime;
              this.dataForm.desc = data.banner.desc;
              this.dataForm.hits = data.banner.hits;
              this.dataForm.shopName = data.banner.shopName;
              //回显图片
              if (this.uploadFile != null) {
                this.uploadFile.url = this.dataForm.images;
              }
              this.fileList.push({
                name: "",
                url: this.dataForm.images,
              });
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      if (this.uploadFile != null) {
        this.dataForm.images = this.uploadFile.url; //设置图片
      }
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/testing/banner/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              status: this.dataForm.status,
              shopId: this.dataForm.shopId,
              title: this.dataForm.title,
              abstractInfo: this.dataForm.abstractInfo,
              images: this.dataForm.images,
              currTime: this.dataForm.currTime,
              desc: this.dataForm.desc,
              hits: this.dataForm.hits,
              shopName: this.dataForm.shopName,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
